/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.openbookformat.tags;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author MUFEED
 */
public class FootnoteTag implements Tag {
    private ArrayList<FnTextTag> fnTags;

    public FootnoteTag() {
        fnTags = new ArrayList<FnTextTag>();
    }
    

    public void addFnTextTag(FnTextTag tag){
        fnTags.add(tag);
    }

    public int size() {
        return fnTags.size();
    }
    
    public boolean isEmpty() {
        return fnTags.isEmpty();
    }

    public FnTextTag getFnTextTag(int idx) {
        return fnTags.get(idx);
    }

    public FnTextTag removeFnTag(int idx) {
        return fnTags.remove(idx);
    }

    @Override
    public String toString() {
        String rVal = "";
        rVal += "<footnote>\n";
        for(int i = 0; i < fnTags.size(); i++)
            rVal += fnTags.get(i).toString() + "\n";
        rVal += "</footnote>";
        return rVal;
    }

    public static final String TAG_PATTERN = 
            "(?s)<footnote>.*?</footnote>";

    public static FootnoteTag parse(String text) throws TagParsingException {
        if(!text.matches(TAG_PATTERN))
            throw new TagParsingException("Parsing error in FootnoteTag:" + text);
        
        FootnoteTag tag = new FootnoteTag();
        Pattern pattern = Pattern.compile(FnTextTag.TAG_PATTERN);
        Matcher matcher = pattern.matcher(text);
        while(matcher.find())
            tag.addFnTextTag(FnTextTag.parse(matcher.group()));
        return tag;
    }

}
